package code.generate.util;

import java.io.*;

/**
 * @author <a href="mailto:HelloHeSir@gmail.com">HeC</a>
 * @date 2018/12/27 11:15
 */
public class FileUtil {

    /**
     * 单文件拷贝
     */
    public static boolean copyFile(File first, File last) {
        try {
            try (InputStream inputStream = new FileInputStream(first);
                 OutputStream outputStream = new FileOutputStream(last)) {
                int byteread;
                byte[] buffer = new byte[1024];
                while ((byteread = inputStream.read(buffer)) != -1) {
                    outputStream.write(buffer, 0, byteread);
                }
                return true;
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 删除文件下所有文件
     */
    public static boolean delAllFile(String path) {
        File file = new File(path);
        if (!file.exists() || !file.isDirectory()) {
            return false;
        }
        String[] tempList = file.list();
        if (null != tempList) {
            File temp;
            for (String tempPath : tempList) {
                if (path.endsWith(File.separator)) {
                    temp = new File(path + tempPath);
                } else {
                    temp = new File(path + File.separator + tempPath);
                }
                if (temp.isFile()) {
                    temp.delete();
                }
                if (temp.isDirectory()) {
                    //先删除文件夹里面的文件
                    delAllFile(path + "/" + tempPath);
                    //再删除空文件夹
                    delFolder(path + "/" + tempPath);
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 删除文件夹
     */
    public static void delFolder(String folderPath) {
        try {
            //删除完里面所有内容
            delAllFile(folderPath);
            File myFilePath = new File(folderPath);
            //删除空文件夹
            myFilePath.delete();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 创建文件
     *
     * @param filePath 路径
     * @param fileName 文件名
     */
    public static void creatFile(String filePath, String fileName) {
        File folder = new File(filePath);
        //文件夹路径不存在
        if (!folder.exists() && !folder.isDirectory()) {
            System.out.println("文件夹路径不存在，创建路径:" + filePath);
            folder.mkdirs();
        } else {
            System.out.println("文件夹路径存在:" + filePath);
        }

        // 如果文件不存在就创建
        File file = new File(folder.getAbsolutePath()+ "/" + fileName);
        if (!file.exists()) {
            System.out.println("文件不存在，创建文件:" + filePath + fileName);
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            System.out.println("文件已存在，文件为:" + filePath + fileName);
        }
    }

}